﻿using System;
using System . Collections . Generic;
using System . IO;
using System . Linq;
using System . Runtime . InteropServices . WindowsRuntime;
using Windows . Foundation;
using Windows . Foundation . Collections;
using Windows . UI . Xaml;
using Windows . UI . Xaml . Controls;
using Windows . UI . Xaml . Controls . Primitives;
using Windows . UI . Xaml . Data;
using Windows . UI . Xaml . Input;
using Windows . UI . Xaml . Media;
using Windows . UI . Xaml . Navigation;

namespace WenceyWang . RichMan4L . App . Pages . Controls
{
	public sealed partial class PlayerConfigListItem : UserControl
	{

		public PlayerConfigListItem ( )
		{
			this . InitializeComponent ( );
			DeleteButton . Click += DeleteButton_Click;
			Loaded += PlayerConfigListItem_Loaded;
			PlayerModelNameList . SelectionChanged += PlayerModelNameList_SelectionChanged;
			ControllerList . SelectionChanged += ControllerList_SelectionChanged;
		}

		private void PlayerConfigListItem_Loaded ( object sender , RoutedEventArgs e )
		{
			foreach ( var item in Players . Models . PlayerModelProxy . GetPlayerModels ( ) )
			{
				PlayerModelNameList . Items . Add ( item . Name );
			}
			foreach ( var item in Enum . GetNames ( typeof ( Players . PlayerType ) ) )
			{
				ControllerList . Items . Add ( item );
			}
			PlayerModelNameList . SelectedIndex = 0;
			ControllerList . SelectedIndex = 0;
		}

		private void PlayerModelNameList_SelectionChanged ( object sender , SelectionChangedEventArgs e )
		{

			PlayerModelName = ( string ) PlayerModelNameList . SelectedItem;
		}

		private void ControllerList_SelectionChanged ( object sender , SelectionChangedEventArgs e )
		{
			Controller = ( string ) ControllerList . SelectedItem;
		}

		public event RoutedEventHandler Delete;

		private void DeleteButton_Click ( object sender , RoutedEventArgs e )
		{
			Delete?.Invoke ( this , new RoutedEventArgs ( ) );
		}

		public string PlayerName
		{
			get { return PlayerNameTextBlock . Text = ( string ) GetValue ( PlayerNameProperty ); }
			set { PlayerNameTextBlock . Text = value; SetValue ( PlayerNameProperty , value ); }
		}

		public static readonly DependencyProperty PlayerNameProperty =
			DependencyProperty . Register ( nameof ( PlayerName ) , typeof ( string ) , typeof ( PlayerConfigListItem ) , new PropertyMetadata ( "" ) );


		public Players . Models . PlayerModelProxy PlayerModel
		{
			get { return ( Players . Models . PlayerModelProxy . GetPlayerModels ( ) ) . First ( ( model ) => model . Name == PlayerModelName ); }
		}



		public string PlayerModelName
		{
			get { return ( string ) ( ( PlayerModelNameButton . Content ) = ( string ) GetValue ( PlayerModelNameProperty ) ); }
			set { PlayerModelNameButton . Content = value; SetValue ( PlayerModelNameProperty , value ); }
		}

		public static readonly DependencyProperty PlayerModelNameProperty =
			DependencyProperty . Register ( nameof ( PlayerModelName ) , typeof ( string ) , typeof ( PlayerConfigListItem ) , new PropertyMetadata ( "" ) );

		public string Controller
		{
			get { return ( string ) ( ControllerButton . Content = ( string ) GetValue ( ControllerProperty ) ); }
			set { ControllerButton . Content = value; SetValue ( ControllerProperty , value ); }
		}

		public static readonly DependencyProperty ControllerProperty =
			DependencyProperty . Register ( nameof ( Controller ) , typeof ( string ) , typeof ( PlayerConfigListItem ) , new PropertyMetadata ( "" ) );



	}
}
